package com.zhuoyue.elmt.job;

import com.zhuoyue.elmt.entity.MaintenanceRecordSz;
import com.zhuoyue.elmt.service.MaintenanceRecordSzService;
import org.apache.commons.lang.StringUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class UpdateMainOverdueJob extends QuartzJobBean {

    @Autowired
    private MaintenanceRecordSzService maintenanceRecordSzService;

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);

        // 所有的没有判断的维保记录
        MaintenanceRecordSz qryMaintenanceRecordSz = new MaintenanceRecordSz();
        qryMaintenanceRecordSz.setOverdueType("0");
        List<MaintenanceRecordSz> maintenanceRecordSzList = maintenanceRecordSzService.findList(qryMaintenanceRecordSz);
        for (MaintenanceRecordSz maintenanceRecordSz : maintenanceRecordSzList) {
            // 查找上一次的维保记录
            List<HashMap> mapList = maintenanceRecordSzService.getPrevMaintenanceRecordSz(maintenanceRecordSz);
            String overdue_type = "";
            for (HashMap hashMap : mapList) {
                String main_type = (String) hashMap.get("main_type");
                Date main_date = (Date) hashMap.get("main_date");

                // 当前维保记录的创建时间和上次维保记录的时间比较
                // 当前创建日期小于上次维保记录的日期说明超时
                if (StringUtils.isNotEmpty(main_type)) {
                    if (main_date != null && maintenanceRecordSz.getCreationDate().compareTo(main_date) > 0) {
                        overdue_type = overdue_type + main_type + ",";
                    }
                }
            }

            if (StringUtils.isEmpty(overdue_type)) {
                overdue_type = "0,";
            }

            maintenanceRecordSz.setOverdueType(overdue_type);
            maintenanceRecordSzService.updateOverdueType(maintenanceRecordSz);
        }

        System.out.println("UpdateMainOverdueJob finish");
    }

}